<template>
  <div class="pana-tab__content">
    <a-form-model
        ref="ruleForm"
        :model="baseInfo"
        :label-col="{ span: 6 }"
        :wrapper-col="{ span: 18 }"
    >
      <a-form-model-item label="ID">
        <a-input
            v-model="baseInfo.id"
            :disabled="idEditDisabled || baseInfo.$type === 'bpmn:Process'"
            clearable
            @change="updateBaseInfo('id')"
        />
      </a-form-model-item>
      <a-form-model-item label="名称">
        <a-input
            v-model="baseInfo.name"
            allow-clear
            @change="updateBaseInfo('name')"
        />
      </a-form-model-item>
      <!--流程的基础属性-->
      <template v-if="baseInfo.$type === 'bpmn:Process'">
        <a-form-model-item label="版本标签">
          <a-input
              v-model="baseInfo.versionTag"
              allow-clear
              @change="updateBaseInfo('versionTag')"
          />
        </a-form-model-item>
        <a-form-model-item label="可执行">
          <a-switch
              v-model="baseInfo.isExecutable"
              checked-children="是"
              un-checked-children="否"
              @change="updateBaseInfo('isExecutable')"
          />
        </a-form-model-item>
      </template>
    </a-form-model>
  </div>
</template>
<script>
export default {
  name: 'BaseInfo',
  props: {
    businessObject: Object,
    type: String,
    idEditDisabled: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      baseInfo: {}
    }
  },
  watch: {
    businessObject: {
      immediate: false,
      handler: function(val) {
        if (val) {
          this.$nextTick(() => this.resetBaseInfo())
        }
      }
    }
  },
  beforeDestroy() {
    this.bpmnElement = null
  },
  methods: {
    resetBaseInfo() {
      this.bpmnElement = window?.bpmnInstances?.bpmnElement
      this.baseInfo = JSON.parse(
        JSON.stringify(this.bpmnElement.businessObject)
      )
    },
    updateBaseInfo(key) {
      const attrObj = Object.create(null)
      attrObj[key] = this.baseInfo[key]
      if (key === 'id') {
        window.bpmnInstances.modeling.updateProperties(this.bpmnElement, {
          id: this.baseInfo[key],
          di: { id: this.baseInfo[key] }
        })
      } else {
        window.bpmnInstances.modeling.updateProperties(
          this.bpmnElement,
          attrObj
        )
      }
    }
  }
}
</script>
